<?php

namespace apps\marriage\app\mp\controller;

use apps\marriage\core\service\UserService;
use apps\marriage\core\service\UserWithdrawCashService;
use think\Request;

class UserWithdrawController extends BaseMarriageMpController
{
    /**
     * @var UserWithdrawCashService
     */
    private $userWithdrawCashService;

    /**
     * @var UserService
     */
    private $userService;

    protected function init()
    {
        parent::init();
        $this->userWithdrawCashService = service('UserWithdrawCash', SERVICE_NAMESPACE);
        $this->userService = service('User', SERVICE_NAMESPACE);
    }

    public function lists(Request  $request)
    {
        $pageSize = $request->param('page_size', 10, 'intval');
        $params = [
            'user_id' => $this->user['id']
        ];
        $res = $this->userWithdrawCashService->pageListByParams($params, $pageSize);
        foreach ($res['data'] as &$item) {
            $item = $this->userWithdrawCashService->format($item);
        }
        return $this->success($res);
    }

    public function apply(Request  $request)
    {
        $amount = $request->param('amount', 0.00, 'floatval');
        $this->userWithdrawCashService->onApply($this->user['id'], $amount);
        return $this->success();
    }
}